<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="状态" prop="status">
            <el-select v-model="filterForm.status" clearable placeholder="请选择">
              <el-option label="0" value="0">全部</el-option>
              <el-option label="6" value="6">已处理</el-option>
              <el-option label="6" value="5">待处理</el-option>
              <el-option label="1" value="1">处理中</el-option>
            </el-select>
        </el-form-item>
      </el-form>
      <el-button  @click="openSubscribeHidden =true">新科目</el-button>
      <el-button  @click="getList()">搜索</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" prop="id" sortable="custom" align="center" width="200">
        <template slot-scope="{row}">
          <span>{{ row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主题">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" width="200">
        <template slot-scope="{row}">
          <span>{{ row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="150px">
        <template slot-scope="{row}">
          <span>{{ row.status.label }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改" width="150px">
        <template slot-scope="{row}">
          <span>{{ row.updatedAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="default" size="mini" @click="openSubscribeHidden = true;subjectForm.subjectID = row.ID">
            新科目
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.page" :limit.sync="filterForm.size" @pagination="getList" />

    <el-dialog
      title="科目档案"
      :visible.sync="openSubscribeHidden"
      :close-on-click-modal="false"
      width="30%">
      <el-form ref="filterForm" :model="subjectForm"  label-position="left" label-width="100px">
        <el-form-item label="科目名称" prop="projectName">
          <el-input v-model="subjectForm.projectName" clearable placeholder="科目名称" style="width: 200px;" class="filter-item"/>
        </el-form-item>
        <el-form-item label="隶属板块" prop="subjectID">
           <el-select v-model="subjectForm.firstsubjectID" placeholder="请选择">
            <el-option
              v-for="item in subjectOptions"
              :key="item.value"
              :label="item.subjectName"
              :value="item.ID">
            </el-option>
          </el-select>
          <el-select v-model="subjectForm.subjectID" placeholder="请选择">
            <el-option
              v-for="item in subsubjectOptions"
              :key="item.value"
              :label="item.subjectName"
              :value="item.ID">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="subjectForm.remark" clearable placeholder="备注" style="width: 200px;" class="filter-item"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="openSubscribeHidden = false;subjectForm.subjectID = 0">取 消</el-button>
        <el-button type="primary" @click="onSubjectDataSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getHost} from '@/utils/socket' // secondary package based on el-pagination

import {getList,saveSubject,removeSubject} from '@/api/finance/subject'

export default {
  name: 'faultinessAdmin',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      socket:"",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      connected:"loading",
      openSubscribeHidden:false,
      subjectOptions:[],
      subsubjectOptions:[],
      allowedRefreshSubsubject:false,
      feelingsKeywordList:[{name:''}],
      subjectForm:{
        subjectID:0
      },
      filterForm: {
        page: 1,
        size: 10,
        title: undefined,
        sort: '+id'
      }
    }
  },
  created() {
    this.getList()
    this.querysubjectBysubjectID(0)
  },
  destroyed(){
    this.close()
  },
  watch:{
    'subjectForm.firstSubjectID'(subjectID){
      this.subsubjectOptions = []
      this.allowedRefreshSubsubject = true
      this.querysubjectBysubjectID(subjectID)
    }
  },
  methods: {
    onSubjectDataSubmit:function(){
      let _this = this
      saveSubject(this.filterForm).then((response) => {
          if (response.code === 200) {
            _this.getList()
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    getList:function(){
      let _this = this;
      console.log(_this.filterForm)
      getList(_this.filterForm).then((response) => {
        console.log(response)
          if (response.code === 200) {
            this.list = response.data.list
            this.total = response.data.total
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    querysubjectBysubjectID:function(subjectID){
      let _this = this
      let filterForm = {

      }
      subjectID = parseInt(subjectID)
      if(isNaN(subjectID)){
        subjectID = 0
      }
      filterForm['subjectID'] = subjectID
      console.log(filterForm)
      getsubjectUsable(filterForm).then((response) => {
          if (response.code === 200) {
            if(subjectID){
              _this.subsubjectOptions = response.data.list
            }else{
            _this.subjectOptions = response.data.list
            }
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    handleUpdate:function(projectData){
      this.openSubscribeHidden = true
      this.subjectForm = projectData
      this.subjectForm['projectID'] = this.subjectForm.ID
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.filterForm.sort = '+id'
      } else {
        this.filterForm.sort = '-id'
      }
      this.handleFilter()
    },
    handleDelete(row, index) {
      removeFaultinessByID(row.ID).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.getList()
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    }
  }
}
</script>
<style>

</style>